package wyp.library.tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class WypJsonParserTool {

	public static String getString4Json(JSONObject __json, boolean __isOption, String... __nameArr) throws JSONException {
		String __return = null;
		JSONException __jsonException = null;
		for (String __str : __nameArr) {
			try {
				__return = __json.getString(__str);
				if (__return != null) {
					break;
				}
			} catch (JSONException e) {
				__return = null;
				__jsonException = e;
			}
		}
		if (__return == null && __isOption == false && __jsonException != null) {
			throw __jsonException;
		}
		return __return;

	}

	public static int getInt4Json(JSONObject __json, int __defaultValue, boolean __isOption, String... __nameArr) throws JSONException {
		long __return = Long.MAX_VALUE;
		JSONException __jsonException = null;
		for (String __str : __nameArr) {
			try {
				__return = __json.getInt(__str);
				if(__return != Long.MAX_VALUE){
					break;
				}
			} catch (JSONException e) {
				__return = Long.MAX_VALUE;
				__jsonException = e;
			}
		}
		if (__return == Long.MAX_VALUE && __isOption == false && __jsonException != null) {
			throw __jsonException;
		}else if(__return == Long.MAX_VALUE && __isOption == true && __jsonException == null){
			__return = __defaultValue;
		}
		
		return (int) __return;

	}

	public static JSONArray getJSONArray(JSONObject __json, boolean __isOption, String... __nameArr) throws JSONException {
		JSONArray __return = null;
		JSONException __jsonException = null;
		for (String __str : __nameArr) {
			try {
				__return = __json.getJSONArray(__str);
				if (__return != null) {
					break;
				}
			} catch (JSONException e) {
				__return = null;
				__jsonException = e;
			}
		}

		if (__return == null && __isOption == false && __jsonException != null) {
			throw __jsonException;
		}
		return __return;
	}
}
